/**
 *
 */

$(function () {
    var email = $('#email');
    var code_phone  = $('#code_phone');
    var get = $('#get');
    var next = $('.next');

    email.blur(function () {
        var emailRegEXP =/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        var email2 = email.val();
        var ok = emailRegEXP.test(email2);
        if (!ok){
            $('#toolphone').text("请输入正确的邮箱");

        }else {
            $.ajax({
                type: "POST",
                url:"ajaxEmail",
                dataType:"text",
                data:{"email":email2},
                success:function (resp) {
                    if (resp!="true"){
                        $('.toolphone2').text("该邮箱已经被注册");
                    }
                }
            })
        }


    })
    email.focus(function () {
        $('.toolphone').text("");
        $('.toolphone2').text("");
    })
    get.click(function () {
        if ($('.toolphone').text() == "" && $('.toolphone2').text() == "") {
            var email2 = email.val();
            var code = email.val();
            $.ajax({
                type: "POST",
                url:"ajaxYanZhenMa",
                dataType:"text",
                data:{"code":code,"email":email2},
                success:function (resp) {
                    alert(resp)
                    changeSendEmailButton();
                }
            })


        }

    })

  next.click(function () {
      var ok = $("#agree").is(':checked');
      if ($("#code_phone").val()==""){
          $(".toolnum2").css("display","block");
      }else if (!ok) {
          alert("请阅读并同意协议");
      }else{
          var code = $('#token').val();
          var code = email.val();
          $.ajax({
              type: "POST",
              url:"ajaxYanZhenMa2",
              dataType:"text",
              data:{"code":code,"codeval":$("#code_phone").val()},
              success:function (resp) {
                 if (resp=="success"){
                     $('.form').submit();
                 }else {
                     alert("验证码错误");
                 }
              }
          })
      }
  })

    $("#code_phone").focus(function () {
        $(".toolnum1").css("display","none");
        $(".toolnum2").css("display","none");
    })

    $('.getProtocol').click(function () {
        $('.show_protocol').show();
    });
    $('.protocol_close').click(function () {
        $('.show_protocol').hide();
    });


	function changeSendEmailButton() {
		var time = setInterval(change, 1000);
		var count = 120;

		function change() {
			count--;
			if (count == 0){
				get.removeAttr('disabled');
				count = 120;
				get.val('发送验证码');
				clearInterval(time);
			} else {
				get.attr('disabled', true);
				get.val('还剩'+count+'秒');
			}
		}
	}



})